﻿using Microsoft.IdentityModel.Logging;
using Simple.ISysRepository;
using Simple.ISysService;
using Simple.SysRepository;
using Simple.Tools;
using Simple.Tools.SnowFlake;
using Simple.Tools.Verify;
using SimpleSaas.Common;
using SimpleSaas.Common.Execption;
using SimpleSaas.Entity;
using SimpleSaas.Entity.Dto;
using SimpleSaas.Entity.Params;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.SysService
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public class SysAppService : ISysAppService
	{
		[Autowired]
		protected ISysAppRepository sysAppRepository { get; set; }

		[Autowired]
		protected ISimpleMapper simpleMapper { get; set; }

		[Autowired]
		protected ISysAppTenantRepository sysAppTenantRepository { get; set; }

		/// <summary>
		///  创建 app
		/// </summary>
		/// <param name="sysAppDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task AddAsync(SysAppDto sysAppDto)
		{
			#region todo 权限验证 之后超级租户的管理员才有权利

			#endregion

			var verify = sysAppDto.Verify();

			if (!verify.Item2)
			{
				throw new BusinessException(verify.Item1);
			}
			if (await sysAppRepository.AnyAsync(app => !app.IsDeleted && app.AppName.Equals(sysAppDto.AppName)))
			{
				throw new BusinessException("app 名称重复!");
			}
			SysAppEntity sysApp = new SysAppEntity();
			simpleMapper.Map(sysAppDto, sysApp);
			await sysAppRepository.CreateAsync(sysApp);
		}

		/// <summary>
		///  更新 app
		/// </summary>
		/// <param name="sysAppDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task UpdateAsync(SysAppDto sysAppDto)
		{
			#region todo 权限验证 之后超级租户的管理员才有权利

			#endregion

			var verify = sysAppDto.Verify();

			if (!verify.Item2)
			{
				throw new BusinessException(verify.Item1);
			}
			if (await sysAppRepository.AnyAsync(app =>app.Id!= sysAppDto.Id.ToInt64() && !app.IsDeleted && app.AppName.Equals(sysAppDto.AppName)))
			{
				throw new BusinessException("app 名称重复!");
			}
			SysAppEntity sysApp =await  sysAppRepository.FirstOrDefaultAsync(app=>app.Id==sysAppDto.Id.ToInt64());

			simpleMapper.Map(sysAppDto, sysApp);

			await sysAppRepository.UpdateAsync(sysApp);
		}

		/// <summary>
		///  删除app
		/// </summary>
		/// <param name="sysAppDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task SoftDeleteAsync(SysAppDto sysAppDto)
		{
			#region todo 权限验证 之后超级租户的管理员才有权利

			#endregion

			if (await sysAppTenantRepository.AnyAsync(t => t.AppId == sysAppDto.Id.ToInt64()))
			{
				throw new BusinessException("删除失败,已经有企业绑定app!");
			}
			SysAppEntity sysApp = await sysAppRepository.FirstOrDefaultAsync(app => app.Id == sysAppDto.Id.ToInt64());
			sysApp.IsDeleted = true;

			await sysAppRepository.SoftDeleteAsync(sysApp);
		}


		/// <summary>
		///  查询
		/// </summary>
		/// <param name="sysAppDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task<SysAppView> GetByIdAsync(SysAppParameter Params)
		{
			#region todo 权限验证 之后超级租户的管理员才有权利

			#endregion

			return await sysAppRepository.Query(q =>
			{
				q.Where(app=>app.Id== Params.Id.ToInt64()&&!app.IsDeleted);
			}).FirstAsync();

		}

		/// <summary>
		///  查询
		/// </summary>
		/// <param name="sysAppDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task<PageList<SysAppView>> GetPageAsync(SysAppParameter Params)
		{
			#region todo 权限验证 之后超级租户的管理员才有权利

			#endregion

			var query= sysAppRepository.Query(q =>
			{
				q.OrderByDescending(app=>app.CreationTime);
			});

			return await PageList<SysAppView>.CreateAsync(query,Params);
		}


	}
}
